﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AzureProvider.MiniSensor;
using AzureProvider.SoupMethods.MiniSensor;

namespace AzureBlogWebRole
{
    public partial class Channel : BlogEngine.Core.Web.Controls.BlogBasePage
    {
        public Guid channelId;

        protected void Page_Load(object sender, EventArgs e)
        {
            AzureProvider.MiniSensor.Channels ch_get = new AzureProvider.MiniSensor.Channels();
            if (!String.IsNullOrEmpty(Request["ChannelId"]))
            {
                channelId = new Guid(Request["ChannelId"]);
                AzureProvider.MiniSensor.Channel ch =ch_get.SelectChannel(channelId);
                L_NameChannel.Text = "Canal : " + ch.Name;
                string MyRss = "<link  rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS "+ch.Name+"\" href=\"/rsschannel.ashx?channelid=" + channelId.ToString() + "\" />";
                LiteralControl control = new LiteralControl(MyRss);
                Page.Header.Controls.Add(control);
            }
            else
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
                List<AzureProvider.MiniSensor.Channel> chs;
                if (HttpRuntime.Cache["widget_channels"] == null)
                {
                    chs = ch_get.GetListChannels();
                    HttpRuntime.Cache["widget_channels"] = chs; 
                }
                else
                {
                    chs = (List < AzureProvider.MiniSensor.Channel >)HttpRuntime.Cache["widget_channels"];
                }
                foreach (var item in chs)
                {
                    Literal1.Text += "<a href=\"/channel.aspx?channelid=" + item.Id.ToString() + "\" >" + item.Name + "</a><br/>";
                }
                //Response.Redirect("/");
            }
               
        }

        protected void Ods_messages_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters[0] = channelId;
        }

        protected void Bt_send_Click(object sender, EventArgs e)
        {
            Message msg = new Message
            {
                Id=Guid.NewGuid(),
                Author=TB_author.Text,
                ChannelId=channelId,
                Content=TB_content.Text,
                DateCreated= DateTime.Now
            };
           Messages msgs = new Messages();
           msgs.InsertMessage(msg);
           GridView1.DataBind();
        }

        
    }
}
